package com.monkey.luffer.springboot.demo.basis.kcc.test.lianji.majiang;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @author jiahong.liu
 */
public class Client {
    public static void main(String[] args) {
        // 主线程执行写操作，发送消息到服务器
        Socket socket = null;
        BufferedOutputStream bos = null;
        BufferedReader bis = null;
        try {
            socket = new Socket("10.12.192.97", 9999);
            System.out.println("服务器连接成功");
            bis = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_16));
            int i = 0;
            while(true) {
                String line;
                while (( line = bis.readLine()) != null) {
                    if (line.contains("A")) {
                        System.out.println(line.substring(2));
                        Scanner scanner = new Scanner(System.in);
                        String cmd = scanner.nextLine();
                        // 转化为字节数组
                        OutputStream out = socket.getOutputStream();
                        PrintWriter writer = new PrintWriter(out, true);

                        // 发送消息
                        // 假设服务器期望每行一个消息
                        writer.println(cmd);

                        // 关闭连接（可选，取决于你的协议和需求）
                        // 注意：如果PrintWriter的autoFlush设置为true，则不需要显式flush
                        // writer.flush();
                    } else {
                        System.out.println(line);
                    }
                }

            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
